<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Stack allocation</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber">
<link rel="prev" href="scheduling.html" title="Scheduling">
<link rel="next" href="stack/valgrind.html" title="Support for valgrind">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="scheduling.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stack/valgrind.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="fiber.stack"></a><a name="stack"></a><a class="link" href="stack.html" title="Stack allocation">Stack allocation</a>
</h2></div></div></div>
<div class="toc"><dl><dt><span class="section"><a href="stack/valgrind.html">Support for valgrind</a></span></dt></dl></div>
<p>
      A <a class="link" href="fiber_mgmt/fiber.html#class_fiber"><code class="computeroutput">fiber</code></a> uses internally an __econtext__ which manages a set of registers
      and a stack. The memory used by the stack is allocated/deallocated via a <span class="emphasis"><em>stack_allocator</em></span>
      which is required to model a <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator
      concept</em></span></a>.
    </p>
<p>
      A <span class="emphasis"><em>stack_allocator</em></span> can be passed to <a class="link" href="fiber_mgmt/fiber.html#fiber_fiber"><code class="computeroutput"><span class="identifier">fiber</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">()</span></code></a> or to <a class="link" href="synchronization/futures/future.html#fibers_async"><code class="computeroutput">fibers::async()</code></a>.
    </p>
<a name="stack_allocator_concept"></a><h4>
<a name="fiber.stack.h0"></a>
      <span><a name="fiber.stack.stack_allocator_concept"></a></span><a class="link" href="stack.html#fiber.stack.stack_allocator_concept">stack-allocator
      concept</a>
    </h4>
<p>
      A <span class="emphasis"><em>stack_allocator</em></span> must satisfy the <span class="emphasis"><em>stack-allocator
      concept</em></span> requirements shown in the following table, in which <code class="computeroutput"><span class="identifier">a</span></code> is an object of a <span class="emphasis"><em>stack_allocator</em></span>
      type, <code class="computeroutput"><span class="identifier">sctx</span></code> is a <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html" target="_top"><code class="computeroutput"><span class="identifier">stack_context</span></code></a>, and <code class="computeroutput"><span class="identifier">size</span></code> is a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code>:
    </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
              <p>
                expression
              </p>
            </th>
<th>
              <p>
                return type
              </p>
            </th>
<th>
              <p>
                notes
              </p>
            </th>
</tr></thead>
<tbody>
<tr>
<td>
              <p>
                <code class="computeroutput"><span class="identifier">a</span><span class="special">(</span><span class="identifier">size</span><span class="special">)</span></code>
              </p>
            </td>
<td>
            </td>
<td>
              <p>
                creates a stack allocator
              </p>
            </td>
</tr>
<tr>
<td>
              <p>
                <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">()</span></code>
              </p>
            </td>
<td>
              <p>
                <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html" target="_top"><code class="computeroutput"><span class="identifier">stack_context</span></code></a>
              </p>
            </td>
<td>
              <p>
                creates a stack
              </p>
            </td>
</tr>
<tr>
<td>
              <p>
                <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span>
                <span class="identifier">sctx</span><span class="special">)</span></code>
              </p>
            </td>
<td>
              <p>
                <code class="computeroutput"><span class="keyword">void</span></code>
              </p>
            </td>
<td>
              <p>
                deallocates the stack created by <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">()</span></code>
              </p>
            </td>
</tr>
</tbody>
</table></div>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
        The implementation of <code class="computeroutput"><span class="identifier">allocate</span><span class="special">()</span></code> might include logic to protect against
        exceeding the context's available stack size rather than leaving it as undefined
        behaviour.
      </p></td></tr>
</table></div>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
        Calling <code class="computeroutput"><span class="identifier">deallocate</span><span class="special">()</span></code>
        with a <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_context.html" target="_top"><code class="computeroutput"><span class="identifier">stack_context</span></code></a> not obtained from
        <code class="computeroutput"><span class="identifier">allocate</span><span class="special">()</span></code>
        results in undefined behaviour.
      </p></td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        The memory for the stack is not required to be aligned; alignment takes place
        inside __econtext__.
      </p></td></tr>
</table></div>
<p>
      See also <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack.html" target="_top">Boost.Context
      stack allocation</a>. In particular, <code class="computeroutput"><span class="identifier">traits_type</span></code>
      methods are as described for <a href="http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_traits.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">context</span><span class="special">::</span><span class="identifier">stack_traits</span></code></a>.
    </p>
<p>
      </p>
<h5>
<a name="class_protected_fixedsize_stack_bridgehead"></a>
  <span><a name="class_protected_fixedsize_stack"></a></span>
  <a class="link" href="stack.html#class_protected_fixedsize_stack">Class
      <code class="computeroutput">protected_fixedsize_stack</code></a>
</h5>
<p>
    </p>
<p>
      <span class="bold"><strong>Boost.Fiber</strong></span> provides the class <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> which
      models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator
      concept</em></span></a>. It appends a guard page at the end of each stack
      to protect against exceeding the stack. If the guard page is accessed (read
      or write operation) a segmentation fault/access violation is generated by the
      operating system.
    </p>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
        Using <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> is expensive.
        Launching a new fiber with a stack of this type incurs the overhead of setting
        the memory protection; once allocated, this stack is just as efficient to
        use as <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a>.
      </p></td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        The appended <code class="computeroutput"><span class="identifier">guard</span> <span class="identifier">page</span></code>
        is <span class="bold"><strong>not</strong></span> mapped to physical memory, only virtual
        addresses are used.
      </p></td></tr>
</table></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">protected_fixedsize</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>

<span class="keyword">struct</span> <span class="identifier">protected_fixedsize</span> <span class="special">{</span>
    <span class="identifier">protected_fixesize</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">());</span>

    <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>

    <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span>
<span class="special">}</span>

<span class="special">}}</span>
</pre>
<p>
      </p>
<h5>
<a name="protected_fixedsize_allocate_bridgehead"></a>
  <span><a name="protected_fixedsize_allocate"></a></span>
  <a class="link" href="stack.html#protected_fixedsize_allocate">Member
      function <code class="computeroutput">allocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span>
            <span class="special">&lt;=</span> <span class="identifier">size</span></code>
            and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span>
            <span class="special">||</span> <span class="special">(</span>
            <span class="identifier">size</span> <span class="special">&lt;=</span>
            <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span>
            <span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Allocates memory of at least <code class="computeroutput"><span class="identifier">size</span></code>
            bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture
            (the stack grows downwards/upwards) the stored address is the highest/lowest
            address of the stack.
          </p></dd>
</dl>
</div>
<p>
      </p>
<h5>
<a name="protected_fixesize_deallocate_bridgehead"></a>
  <span><a name="protected_fixesize_deallocate"></a></span>
  <a class="link" href="stack.html#protected_fixesize_deallocate">Member
      function <code class="computeroutput">deallocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span> <span class="special">&lt;=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Deallocates the stack space.
          </p></dd>
</dl>
</div>
<p>
      </p>
<h5>
<a name="class_pooled_fixedsize_stack_bridgehead"></a>
  <span><a name="class_pooled_fixedsize_stack"></a></span>
  <a class="link" href="stack.html#class_pooled_fixedsize_stack">Class
      <code class="computeroutput">pooled_fixedsize_stack</code></a>
</h5>
<p>
    </p>
<p>
      <span class="bold"><strong>Boost.Fiber</strong></span> provides the class <a class="link" href="stack.html#class_pooled_fixedsize_stack"><code class="computeroutput">pooled_fixedsize_stack</code></a> which
      models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator
      concept</em></span></a>. In contrast to <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> it
      does not append a guard page at the end of each stack. The memory is managed
      internally by <a href="http://www.boost.org/doc/libs/release/libs/pool/doc/html/boost/pool.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">pool</span><span class="special">&lt;&gt;</span></code></a>.
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">pooled_fixedsize_stack</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>

<span class="keyword">struct</span> <span class="identifier">pooled_fixedsize_stack</span> <span class="special">{</span>
    <span class="identifier">pooled_fixedsize_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">stack_size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">next_size</span> <span class="special">=</span> <span class="number">32</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_size</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>

    <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>

    <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span>
<span class="special">}</span>

<span class="special">}}</span>
</pre>
<p>
      </p>
<h5>
<a name="pooled_fixedsize_bridgehead"></a>
  <span><a name="pooled_fixedsize"></a></span>
  <a class="link" href="stack.html#pooled_fixedsize">Constructor</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="identifier">pooled_fixedsize_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">stack_size</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">next_size</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_size</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span>
            <span class="special">||</span> <span class="special">(</span>
            <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span>
            <span class="special">&gt;=</span> <span class="identifier">stack_size</span><span class="special">)</span></code> and <code class="computeroutput"><span class="number">0</span>
            <span class="special">&lt;</span> <span class="identifier">next_size</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Allocates memory of at least <code class="computeroutput"><span class="identifier">stack_size</span></code>
            bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture
            (the stack grows downwards/upwards) the stored address is the highest/lowest
            address of the stack. Argument <code class="computeroutput"><span class="identifier">next_size</span></code>
            determines the number of stacks to request from the system the first
            time that <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
            needs to allocate system memory. The third argument <code class="computeroutput"><span class="identifier">max_size</span></code>
            controls how much memory might be allocated for stacks &#8212; a value of zero
            means no upper limit.
          </p></dd>
</dl>
</div>
<p>
      </p>
<h5>
<a name="pooled_fixedsize_allocate_bridgehead"></a>
  <span><a name="pooled_fixedsize_allocate"></a></span>
  <a class="link" href="stack.html#pooled_fixedsize_allocate">Member
      function <code class="computeroutput">allocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span>
            <span class="special">||</span> <span class="special">(</span>
            <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span>
            <span class="special">&gt;=</span> <span class="identifier">stack_size</span><span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Allocates memory of at least <code class="computeroutput"><span class="identifier">stack_size</span></code>
            bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture
            (the stack grows downwards/upwards) the stored address is the highest/lowest
            address of the stack.
          </p></dd>
</dl>
</div>
<p>
      </p>
<h5>
<a name="pooled_fixesize_deallocate_bridgehead"></a>
  <span><a name="pooled_fixesize_deallocate"></a></span>
  <a class="link" href="stack.html#pooled_fixesize_deallocate">Member
      function <code class="computeroutput">deallocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span><span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Deallocates the stack space.
          </p></dd>
</dl>
</div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        This stack allocator is not thread safe.
      </p></td></tr>
</table></div>
<p>
      </p>
<h5>
<a name="class_fixedsize_stack_bridgehead"></a>
  <span><a name="class_fixedsize_stack"></a></span>
  <a class="link" href="stack.html#class_fixedsize_stack">Class
      <code class="computeroutput">fixedsize_stack</code></a>
</h5>
<p>
    </p>
<p>
      <span class="bold"><strong>Boost.Fiber</strong></span> provides the class <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a> which
      models the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator
      concept</em></span></a>. In contrast to <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> it
      does not append a guard page at the end of each stack. The memory is simply
      managed by <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">malloc</span><span class="special">()</span></code>
      and <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">free</span><span class="special">()</span></code>.
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">context</span><span class="special">/</span><span class="identifier">fixedsize_stack</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>

<span class="keyword">struct</span> <span class="identifier">fixedsize_stack</span> <span class="special">{</span>
    <span class="identifier">fixedsize_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">());</span>

    <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>

    <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span>
<span class="special">}</span>

<span class="special">}}</span>
</pre>
<p>
      </p>
<h5>
<a name="fixedsize_allocate_bridgehead"></a>
  <span><a name="fixedsize_allocate"></a></span>
  <a class="link" href="stack.html#fixedsize_allocate">Member function
      <code class="computeroutput">allocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span>
            <span class="special">&lt;=</span> <span class="identifier">size</span></code>
            and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span>
            <span class="special">||</span> <span class="special">(</span>
            <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span>
            <span class="special">&gt;=</span> <span class="identifier">size</span><span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Allocates memory of at least <code class="computeroutput"><span class="identifier">size</span></code>
            bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture
            (the stack grows downwards/upwards) the stored address is the highest/lowest
            address of the stack.
          </p></dd>
</dl>
</div>
<p>
      </p>
<h5>
<a name="fixesize_deallocate_bridgehead"></a>
  <span><a name="fixesize_deallocate"></a></span>
  <a class="link" href="stack.html#fixesize_deallocate">Member
      function <code class="computeroutput">deallocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span><span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Deallocates the stack space.
          </p></dd>
</dl>
</div>
<p>
      <a name="segmented"></a></p>
<h5>
<a name="class_segmented_stack_bridgehead"></a>
  <span><a name="class_segmented_stack"></a></span>
  <a class="link" href="stack.html#class_segmented_stack">Class
      <code class="computeroutput">segmented_stack</code></a>
</h5>
<p>
    </p>
<p>
      <span class="bold"><strong>Boost.Fiber</strong></span> supports usage of a <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a>,
      i.e. the stack grows on demand. The fiber is created with a minimal stack size
      which will be increased as required. Class <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a> models
      the <a class="link" href="stack.html#stack_allocator_concept"><span class="emphasis"><em>stack-allocator concept</em></span></a>.
      In contrast to <a class="link" href="stack.html#class_protected_fixedsize_stack"><code class="computeroutput">protected_fixedsize_stack</code></a> and
      <a class="link" href="stack.html#class_fixedsize_stack"><code class="computeroutput">fixedsize_stack</code></a> it creates a stack which grows on demand.
    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        Segmented stacks are currently only supported by <span class="bold"><strong>gcc</strong></span>
        from version <span class="bold"><strong>4.7</strong></span> and <span class="bold"><strong>clang</strong></span>
        from version <span class="bold"><strong>3.4</strong></span> onwards. In order to use
        a <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a> <span class="bold"><strong>Boost.Fiber</strong></span>
        must be built with property <code class="computeroutput"><span class="identifier">segmented</span><span class="special">-</span><span class="identifier">stacks</span></code>,
        e.g. <span class="bold"><strong>toolset=gcc segmented-stacks=on</strong></span> and
        applying BOOST_USE_SEGMENTED_STACKS at b2/bjam command line.
      </p></td></tr>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        Segmented stacks can only be used with callcc() using property <code class="computeroutput"><span class="identifier">context</span><span class="special">-</span><span class="identifier">impl</span><span class="special">=</span><span class="identifier">ucontext</span></code>.
      </p></td></tr>
</table></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fiber</span><span class="special">/</span><span class="identifier">segmented_stack</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">namespace</span> <span class="identifier">fibers</span> <span class="special">{</span>

<span class="keyword">struct</span> <span class="identifier">segmented_stack</span> <span class="special">{</span>
    <span class="identifier">segmented_stack</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">stack_size</span> <span class="special">=</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">default_size</span><span class="special">());</span>

    <span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>

    <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;);</span>
<span class="special">}</span>

<span class="special">}}</span>
</pre>
<p>
      </p>
<h5>
<a name="segmented_allocate_bridgehead"></a>
  <span><a name="segmented_allocate"></a></span>
  <a class="link" href="stack.html#segmented_allocate">Member function
      <code class="computeroutput">allocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="identifier">stack_context</span> <span class="identifier">allocate</span><span class="special">();</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span>
            <span class="special">&lt;=</span> <span class="identifier">size</span></code>
            and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span>
            <span class="special">||</span> <span class="special">(</span>
            <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span>
            <span class="special">&gt;=</span> <span class="identifier">size</span><span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Allocates memory of at least <code class="computeroutput"><span class="identifier">size</span></code>
            bytes and stores a pointer to the stack and its actual size in <code class="computeroutput"><span class="identifier">sctx</span></code>. Depending on the architecture
            (the stack grows downwards/upwards) the stored address is the highest/lowest
            address of the stack.
          </p></dd>
</dl>
</div>
<p>
      </p>
<h5>
<a name="segmented_deallocate_bridgehead"></a>
  <span><a name="segmented_deallocate"></a></span>
  <a class="link" href="stack.html#segmented_deallocate">Member
      function <code class="computeroutput">deallocate</code>()</a>
</h5>
<p>
    </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">(</span> <span class="identifier">stack_context</span> <span class="special">&amp;</span> <span class="identifier">sctx</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl>
<dt><span class="term">Preconditions:</span></dt>
<dd><p>
            <code class="computeroutput"><span class="identifier">sctx</span><span class="special">.</span><span class="identifier">sp</span></code> is valid, <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">minimum_size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span></code> and <code class="computeroutput"><span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">is_unbounded</span><span class="special">()</span> <span class="special">||</span> <span class="special">(</span> <span class="identifier">traits_type</span><span class="special">::</span><span class="identifier">maximum_size</span><span class="special">()</span> <span class="special">&gt;=</span> <span class="identifier">sctx</span><span class="special">.</span><span class="identifier">size</span><span class="special">)</span></code>.
          </p></dd>
<dt><span class="term">Effects:</span></dt>
<dd><p>
            Deallocates the stack space.
          </p></dd>
</dl>
</div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        If the library is compiled for segmented stacks, <a class="link" href="stack.html#class_segmented_stack"><code class="computeroutput">segmented_stack</code></a> is
        the only available stack allocator.
      </p></td></tr>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="scheduling.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="stack/valgrind.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
